package com.test.nnooolwdeaer.activity_fragment_wooden;

import android.Manifest;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;

import androidx.annotation.NonNull;

import com.test.nnooolwdeaer.activity_fragment_wooden.info_wooden.BindBankAccountActivity_Cote_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.info_wooden.InfoUploadActivity_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.loan_wooden.LoanApplyActivity_wooden;
import com.test.nnooolwdeaer.bean_wooden.StaticVariate_wooden;
import com.test.nnooolwdeaer.bean_wooden.process_wooden.ProcessInfo_wooden;
import com.test.nnooolwdeaer.constant_wooden.Constant_wooden;
import com.test.nnooolwdeaer.R;
import com.test.nnooolwdeaer.activity_fragment_wooden.base_wooden.BaseActivity_wooden;
import com.test.nnooolwdeaer.dialog_wooden.TipsDialog_wooden;
import com.test.nnooolwdeaer.util_wooden.analytics_wooden.util_wooden.AnalyticsUtil_wooden;
import com.test.nnooolwdeaer.util_wooden.helper_wooden.UserInfoHelper_wooden;
import com.test.nnooolwdeaer.util_wooden.http_wooden.HttpManager_wooden;
import com.test.nnooolwdeaer.view_wooden.widget_wooden.TitleView_wooden;

import java.util.List;

import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;


public class ApplyPermissionActivity_wooden extends BaseActivity_wooden {

    private final int Code = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply_permission_wooden);

        TitleView_wooden titleView = findViewById(R.id.apply_permission_title_view);
        CheckBox readCb = findViewById(R.id.apply_permission_cb_read);
        Button denyBtn = findViewById(R.id.apply_permission_btn_deny);
        Button agreeBtn = findViewById(R.id.apply_permission_btn_agree);

        titleView.getBackIb_wooden().setOnClickListener(v -> {
            HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint("PERMISSION_BACK_CLICK");
            onBackPressed();
        });

        denyBtn.setOnClickListener(v -> {
            HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint("PERMISSION_DENY_CLICK");
            HttpManager_wooden.getInstance_wooden().requestUserProcess_wooden();
        });

        agreeBtn.setOnClickListener(v -> {
            HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint("PERMISSION_AGREE_CLICK");
            if (readCb.isChecked()){
                EasyPermissions.requestPermissions(this, getString(R.string.launch_permissions_camera), Code, Manifest.permission.CAMERA);
            }else {
                new TipsDialog_wooden(this,() -> {
                    readCb.setChecked(true);
                    EasyPermissions.requestPermissions(this, getString(R.string.launch_permissions_camera), Code, Manifest.permission.CAMERA);
                }).show();
            }
        });

    }

    @Override
    public void onUserProcessResponse_wooden() {

        try {
            if (UserInfoHelper_wooden.getInstance_wooden().didLogin_wooden()) {
                AnalyticsUtil_wooden.INSTANCE.saveDeviceS2SInfo();
            }
        } catch (Exception e) {
            if (Constant_wooden.Debug) e.printStackTrace();
        }

        //科特的绑卡环节
        if (StaticVariate_wooden.Select_Country_wooden.equalsIgnoreCase(Constant_wooden.Country_Cote_wooden) &&
                ProcessInfo_wooden.getInstance_wooden().getStepName_wooden(0).equalsIgnoreCase("account_info")) {
            startActivity_wooden(BindBankAccountActivity_Cote_wooden.class);
        } else if (ProcessInfo_wooden.getInstance_wooden().getStepsSize_wooden() > 0) {
            startActivity_wooden(InfoUploadActivity_wooden.class);
        } else {
            //如果不是处理还款流程、状态流程、认证流程，直接去借款确认页
            startActivity_wooden(LoanApplyActivity_wooden.class);
        }

        finish();
    }


    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        if (requestCode == Code){
            HttpManager_wooden.getInstance_wooden().requestUserProcess_wooden();
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {

        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms))
            new AppSettingsDialog.Builder(this).build().show();
        else if (requestCode == Code) {
            HttpManager_wooden.getInstance_wooden().requestUserProcess_wooden();
        }
    }

    @Override
    public void onRationaleAccepted(int requestCode) {
        if (requestCode == Code)
            EasyPermissions.requestPermissions(this, getString(R.string.launch_permissions_camera), Code, Manifest.permission.CAMERA);
    }

    @Override
    public void onRationaleDenied(int requestCode) {}


}